/**   
* @Title: ConfigFilePath.java 
* @Package: com.ifuntimes.utils
* @Description: TODO
* @author:YuHai
* @Email: it@ifuntimes.com  
* @date 2017年09月05日 下午8:21:33 
* @version V1.0   
*/
package com.ifuntimes.utils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;

/**
 * @ClassName: ConfigFilePath
 * @Description: TODO
 * @author YuHai it@ifuntimes.com 
 * @date 2017年09月05日 下午8:21:33
 * 
 */
class ConfigFilePath {
	public Config createConfigObject(String configPath) throws IOException {
		Config config = null;
		if (configPath == null) {
			config = ConfigFactory.defaultReference();
		} else {
			if (configPath.startsWith("classpath:")) {
				if (configPath.length() < 11) {
					throw new IllegalArgumentException("configPath seems not right");
				}
				try (InputStream in = ClassLoader.getSystemClassLoader()
						.getResourceAsStream(configPath.substring(10))) {
					config = ConfigFactory.parseReader(new InputStreamReader(in));
				} catch (IOException e) {
					throw e;
				}
			} else {
				File configFile = new File(configPath);				
				if (!configFile.exists() || configFile.isDirectory()) {
					throw new IllegalArgumentException("Config file doesn't exist or it's directory");
				}
				config = ConfigFactory.parseFile(configFile);
			}
			config.withFallback(ConfigFactory.defaultReference());
		}
		return config;
	}
}
